package com.zsk.sso.config.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        // 如果是登录有关操作的，不拦截
        String path = req.getRequestURI();
        // 1. session还有效
        HttpSession session = req.getSession();
        String token = (String)session.getAttribute("token");
        if (token != null) {
            // 放行
            chain.doFilter(request, response);
            return;
        }
        if (path.toLowerCase().contains("login") 
            || path.endsWith(".css") 
            || path.endsWith(".js")
            || path.endsWith(".ico")) {
            // 放行
            chain.doFilter(request, response);
            return;
        } else {
            System.out.println("path:" + path);
            if (path.endsWith("verify")) {
                // 放行
                chain.doFilter(request, response);
                return;
            } else {
                resp.sendRedirect("login");
            }
        }

    }

    @Override
    public void destroy() {

    }

}
